package com.wisdomparking.control.data;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.wisdomparking.bean.Orders;
import com.wisdomparking.bean.Parking;
import com.wisdomparking.bean.Rawdata;
import com.wisdomparking.bean.VehicleInfo;
import com.wisdomparking.config.result.Message;
import com.wisdomparking.control.BaseController;
import com.wisdomparking.service.OrdersService;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.RawdataService;
import com.wisdomparking.service.UserService;
import com.wisdomparking.service.VehicleInfoService;
import com.wisdomparking.tools.ToolService;
import com.wisdomparking.tools.ToolString;
import com.wisdomparking.tools.ToolValid;

/**
 * 对外服务接口
 */
@RestController
@RequestMapping("/data/serviceCenter")
public class ServiceController extends BaseController{
	
	@Autowired OrdersService ordersService;
	@Autowired VehicleInfoService vehicleInfoService;
	@Autowired ParkingService parkingService;
	@Autowired UserService userService;
	@Autowired RawdataService rawdataService;
	
	/**
	 * 生成订单 - 抬杆通知接口【<<====】
	 * 
	 * @param plateNumber		车牌
	 * @param identification		车场唯一标识
	 * @param memberFlag			是否车场会员标记
	 * @return
	 */
	@RequestMapping("/generateOrder")
	public Message generateOrder(String plateNumber, String identification, Boolean memberFlag){
		boolean flag = false;
		
		if(ToolValid.isNotEmpty(plateNumber) && ToolValid.isNotEmpty(identification)){
			VehicleInfo vehicleInfo = vehicleInfoService.getVehicleInfoByPlateNumber(plateNumber);
			Parking parking = parkingService.getParkingByIdentification(identification);
			
			Date date = new Date();
			Orders order = new Orders();
			
			if(ToolValid.isNotNull(vehicleInfo)){
				order.setUserId(vehicleInfo.getUserId());
			}
			else{
				// 系统不存在车辆数据
				vehicleInfo = new VehicleInfo();
				vehicleInfo.setPlateNumber(plateNumber);
				vehicleInfo.setCreateTime(new Date());
				
				vehicleInfoService.addVehicleInfo(vehicleInfo, true);
			}
			order.setMemberFlag(memberFlag);
			order.setOrderNumber(ToolString.generateSingleNumber("JHZL_"));
			order.setParkingId(parking.getId());
			order.setParkingAddress(parking.getAddress());
			order.setParkingName(parking.getName());
			order.setStartTime(date);
			order.setIdentification(identification);
			order.setPlateNumber(plateNumber);
			order.setStatus(0);
			order.setCreateTime(date);
			
			flag = ordersService.addOrders(order, true);
		}
		
		return new Message(flag);
	}
	
	
	/**
	 * 结束订单 - 落杆通知接口 【同：手动通知结束订单】【<<====】
	 * 
	 * @param plateNumber		车牌号
	 * @param identification		车场唯一标识
	 * @param paymentAmount		支付金额
	 * @param payMode			支付方式
	 * @return
	 */
	@RequestMapping("/closureOrder")
	public Message closureOrder(String plateNumber, String identification, String paymentAmount, String payMode){
		boolean flag = false;
		
		if(ToolValid.isNotEmpty(plateNumber) && ToolValid.isNotEmpty(identification)){
			Map<String, Object> m = new HashMap<>();
			m.put("plateNumber", plateNumber);
			m.put("statusLess", 2);
			m.put("orderByField", "id");
			m.put("orderByFlag", "desc");
			
			List<Orders> list = ordersService.getOrdersList(m);
			
			if(null!=list && list.size()>0){
				Orders orders = list.get(0);
				if(ToolValid.isNotNull(orders)){
					orders.setAmount(String.valueOf(Float.parseFloat(paymentAmount) * 100).split("\\.")[0]);
					orders.setPayMode(payMode);
					orders.setEndTime(new Date());
					orders.setStatus(2);
					
					flag = ordersService.updateOrders(orders, true);
				}
			}
			
		}
		
		return new Message(flag);
	}
	
	
	/**
	 * 车场实时信息 - 心跳数据【<<====】
	 * 
	 * @param map	其中必须包含车场唯一标识: identification
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/heartbeatPackets")
	public Boolean heartbeatPackets(String realTimeData){
		boolean flag = false;
		
		if(ToolValid.isNotEmpty(realTimeData)){
			
			Map<String, Object> map = new HashMap<String, Object>();
			map = new Gson().fromJson(realTimeData, map.getClass());
			
			String identification = String.valueOf(map.get("identification"));
			if(ToolValid.isNotEmpty(identification)){
				boolean mark = false;
				Rawdata rawdata = rawdataService.getRawdataByIdentification(identification);
				
				if(ToolValid.isNull(rawdata)){
					rawdata = new Rawdata();
					mark = true;
				}
				
				rawdata.setIdentification(identification);
				rawdata.setPutInStorageMark(false);
				rawdata.setRealTimeData(new Gson().toJson(map));
				rawdata.setUpdateTime(new Date());
				
				if(mark){
					flag = rawdataService.addRawdata(rawdata, true);
				}
				else{
					flag = rawdataService.updateRawdata(rawdata, true);
				}
				
			}
		}
		return flag;
	}
	
	/**
	 * 通过 App 支付完成后通知车场做抬杠动作【====>>】
	 * 
	 * @param identification	车场唯一标识
	 * @param plateNumber		车牌号
	 */
	@RequestMapping("/payfinishedSendParking")
	public Boolean payfinishedSendParking(String identification, String plateNumber){
		
		return ToolService.payfinishedSendParking(parkingService, identification, plateNumber);
	}
	
}
